package com.educhains.web.ctrl.backend;


import com.alibaba.fastjson.JSONObject;
import com.educhains.module.demo.vo.Page;
import com.educhains.module.user.service.UserService;
import com.educhains.module.user.vo.User;
import com.educhains.web.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserMaintain {

    private static final String  TYPE_PATH = "/WEB-INF/jsp/backend/maintain/panelList/userList.jsp";
    private static final String  TYPE = "user";

    @Autowired
    private UserService userService = null;


//    @RequestMapping(value = "/maintain/userSearch")
//    public String userSearch(HttpServletRequest request) {
//
//        String emailFragment = request.getParameter("email_fragment");
//        String pageNum = request.getParameter("p") == null ? "1" : request.getParameter("p");
//        Page userPage = userService.searchByEmailPage(Integer.valueOf(pageNum), emailFragment);
//
//
//        request.setAttribute("type", TYPE);
//        request.setAttribute("typePath", TYPE_PATH);
//        request.setAttribute("userPage", userPage);
//        request.setAttribute("emailFragment", emailFragment);
//        return "frontend/maintain/operationPanel";
//    }

    @RequestMapping(value = "/maintain/userManage")
    public String userManage(HttpServletRequest request) {

        String emailFragment = request.getParameter("email_fragment");
        String pageNum = request.getParameter("p") == null ? "1" : request.getParameter("p");

        Page userPage;
        if(emailFragment == null || emailFragment.equals("") || emailFragment.equals("undefined")) {

            userPage = userService.showUserPage(Integer.valueOf(pageNum));
        } else {

            userPage = userService.searchByEmailPage(Integer.valueOf(pageNum), emailFragment);
        }


        request.setAttribute("type", TYPE);
        request.setAttribute("typePath", TYPE_PATH);
        request.setAttribute("userPage", userPage);
        request.setAttribute("emailFragment", emailFragment);

        return "backend/maintain/operationPanel";
    }

    @ResponseBody
    @RequestMapping(value = "/maintain/userModify")
    public String userModify(HttpServletRequest request) {

        String emailContent = request.getParameter("email_content");
//        String nameContent = request.getParameter("name_content"); //获取前台用户昵称，修改功能待定
        int classification = Integer.parseInt(request.getParameter("classification"));

        User user = userService.getUserByEmail(emailContent);
        user.setUserAuthority(classification);
        userService.updateUserAuthority(user);

        return returnBody(true, "modify successfully");
    }

    private static String returnBody(boolean success, String msg) {
        JSONObject json = new JSONObject();
        if (success) {
            json.put("success", true);
            json.put("msg", msg);
        } else {
            json.put("success", false);
            json.put("msg", msg);
        }
        //return json.toJSONString();
        return JsonUtils.toJSONString(json);
    }
}
